﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;
using Auto.Data.DBUtility;//请先添加引用
using Auto.Components;
using Model = Auto.Data.Systems.Model;
namespace Auto.Data.Systems.DAL
{
    /// <summary>
    ///  Data Access Class SubDivision。
    /// </summary>
    public class SubDivision
    {
        #region --New--
        /// <sumary> 
        /// Constructed Function 
        /// </sumary> 
        public SubDivision()
        {
        }
        /// <summary>
        /// Get Model
        /// </summary>
        /// <param name="objKey">key of Table</param>
        public Model.SubDivision GetModel(params object[] objKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT TOP 1 RecID, DivID, SubDivID, ShortDivName, DivName, ParentID, Path, Title, Email, Phone, Contact");
            strSql.Append(" FROM _SubDivision");
            strSql.Append(" WHERE DivID=@DivID AND SubDivID=@SubDivID");
            SqlParameter[] parameters = SetParametersValue(objKey);
            Model.SubDivision model;
            DataRow dr = DbHelper.GetRecord(strSql.ToString(), parameters);
            if (dr != null)
            {
                model = new Model.SubDivision(dr);
                return model;
            }
            else
            {
                return null;
            }
        }
        #endregion --New--
        #region --Operate--
        /// <summary>
        /// Is Exists Or Not
        /// </summary>
        public bool Exists(params object[] objKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT COUNT(1) FROM _SubDivision  ");
            strSql.Append(" WHERE DivID=@DivID AND SubDivID=@SubDivID");
            SqlParameter[] parameters = SetParametersValue(objKey);
            return DbHelper.Exists(strSql.ToString(), parameters);
        }
        #endregion --Operate--
        #region --SQL--
        /// <summary>
        /// Add One Record
        /// </summary>
        public void Add(Model.SubDivision model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO _SubDivision(");
            strSql.Append("DivID,SubDivID,ShortDivName,DivName,ParentID,Path,Title,Email,Phone,Contact)");
            strSql.Append(" VALUES (");
            strSql.Append("@DivID,@SubDivID,@ShortDivName,@DivName,@ParentID,@Path,@Title,@Email,@Phone,@Contact)");
            SqlParameter[] parameters = SetParametersValue(model);
            try
            {
                object obj = DbHelper.ExecuteNonQuery(strSql.ToString(), parameters);
                if (obj == null)
                {
                    throw new Exception("Error When Insert Data Into SubDivision.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Update One Record
        /// </summary>
        public void Update(Model.SubDivision model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE _SubDivision SET ");
            strSql.Append("DivID=@DivID,");
            strSql.Append("ShortDivName=@ShortDivName,");
            strSql.Append("DivName=@DivName,");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("Path=@Path,");
            strSql.Append("Title=@Title,");
            strSql.Append("Email=@Email,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Contact=@Contact");
            strSql.Append(" WHERE DivID=@DivID AND SubDivID=@SubDivID ");
            SqlParameter[] parameters = SetParametersValue(model);
            DbHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// Delete One Record
        /// </summary>
        public void Delete(params object[] objKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE _SubDivision ");
            strSql.Append(" WHERE DivID=@DivID AND SubDivID=@SubDivID");
            SqlParameter[] parameters = SetParametersValue(objKey);
            DbHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion --SQL--
        #region --Get Data--
        /// <sumary> 
        /// Get Data By Primary Key 
        /// </sumary> 
        /// <param name="objKey">Key of Table</param> 
        /// <returns>DataTable</returns> 
        public DataTable GetDataByKeyValue(params object[] objKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT RecID, DivID, SubDivID, ShortDivName, DivName, ParentID, Path, Title, Email, Phone, Contact");
            strSql.Append(" FROM _SubDivision");
            strSql.Append(" WHERE DivID=@DivID AND SubDivID=@SubDivID");
            SqlParameter[] parameters = SetParametersValue(objKey);
            return DbHelper.ExecuteQuery(strSql.ToString(), parameters).Tables[0];
        }
        /// <sumary> 
        /// Get Data List 
        /// </sumary> 
        /// <param name="strWhere">strWhere Format: Colname='?' AND ColName2='?'... </param> 
        /// <returns>DataTable</returns> 
        public DataTable GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  RecID, DivID, SubDivID, ShortDivName, DivName, ParentID, Path, Title, Email, Phone, Contact");
            strSql.Append(" FROM _SubDivision ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            return DbHelper.ExecuteQuery(strSql.ToString()).Tables[0];
        }
        /// <sumary> 
        /// Get TOP Rows Record 
        /// </sumary> 
        /// <param name="top">TOP Rows</param> 
        /// <param name="strWhere">strWhere Format: ColName1='?' AND ColName2='?' ..</param> 
        /// <param name="fieldOrder">Field Order By. Format: ColName1,ColName2...</param> 
        /// <returns>DataTable:TOP Rows</returns> 
        public DataTable GetList(int top, string strWhere, string fieldOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT ");
            if (top > 0)
            {
                strSql.Append(" TOP " + top.ToString());
            }
            strSql.Append(" RecID, DivID, SubDivID, ShortDivName, DivName, ParentID, Path, Title, Email, Phone, Contact ");
            strSql.Append(" FROM _SubDivision ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ORDER BY " + fieldOrder);
            return DbHelper.ExecuteQuery(strSql.ToString()).Tables[0];
        }
        /// <sumary> 
        /// Convert Table to Model List Model.SubDivision
        /// </sumary> 
        /// <param name="dt">DataTable Of SubDivision</param> 
        /// <returns>List Model.SubDivision </returns> 
        public List<Model.SubDivision> DataTableToList(DataTable dt)
        {
            List<Model.SubDivision> modelList = new List<Model.SubDivision>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.SubDivision model;
                for (int n = 0; n < rowsCount; n++)
                {
                    DataRow dr = dt.Rows[n];
                    model = new Model.SubDivision(dr);
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// Get Data List By Page
        /// </summary>
        public DataTable GetListByPage(int pageSize, int pageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
		   new SqlParameter("@tblName", SqlDbType.VarChar, 255),
		   new SqlParameter("@fldName", SqlDbType.VarChar, 255),
		   new SqlParameter("@PageSize", SqlDbType.Int),
		   new SqlParameter("@PageIndex", SqlDbType.Int),
		   new SqlParameter("@IsReCount", SqlDbType.Bit),
		   new SqlParameter("@OrderType", SqlDbType.Bit),
		   new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
		   };
            parameters[0].Value = "_SubDivision";
            parameters[1].Value = "RecID";
            parameters[2].Value = pageSize;
            parameters[3].Value = pageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelper.ExecuteQueryProcedure("UP_GetRecordByPage", parameters, "ds").Tables[0];
        }

        #endregion --Get Data--
        #region ----PrivateSet--
        /// <sumary> 
        /// Set Parameters Value 
        /// </sumary> 
        /// <param name="model">Model</param> 
        /// <returns>Parameters</returns> 
        private SqlParameter[] SetParametersValue(Model.SubDivision model)
        {
            SqlParameter[] parameters = { 
                                    		StrHelper.MakeParam("@DivID",SqlDbType.VarChar,20,ParameterDirection.Input,model.DivID),
                                    		StrHelper.MakeParam("@SubDivID",SqlDbType.VarChar,20,ParameterDirection.Input,model.SubDivID),
                                    		StrHelper.MakeParam("@ShortDivName",SqlDbType.NVarChar,20,ParameterDirection.Input,model.ShortDivName),
                                    		StrHelper.MakeParam("@DivName",SqlDbType.NVarChar,100,ParameterDirection.Input,model.DivName),
                                    		StrHelper.MakeParam("@ParentID",SqlDbType.VarChar,20,ParameterDirection.Input,model.ParentID),
                                    		StrHelper.MakeParam("@Path",SqlDbType.VarChar,200,ParameterDirection.Input,model.Path),
                                    		StrHelper.MakeParam("@Title",SqlDbType.NVarChar,100,ParameterDirection.Input,model.Title),
                                    		StrHelper.MakeParam("@Email",SqlDbType.NVarChar,50,ParameterDirection.Input,model.Email),
                                    		StrHelper.MakeParam("@Phone",SqlDbType.NVarChar,50,ParameterDirection.Input,model.Phone),
                                    		StrHelper.MakeParam("@Contact",SqlDbType.NVarChar,50,ParameterDirection.Input,model.Contact),

                                 		};
            return parameters;
        }
        /// <sumary> 
        /// Set Parameter Value of Key 
        /// </sumary> 
        /// <param name="objKey">Key Values</param> 
        /// <returns>Parameters</returns> 
        private SqlParameter[] SetParametersValue(object[] objKey)
        {
            SqlParameter[] parameters = { 
                                    		StrHelper.MakeParam("@DivID",SqlDbType.VarChar,20,ParameterDirection.Input,objKey[0]),
                                    		StrHelper.MakeParam("@SubDivID",SqlDbType.VarChar,20,ParameterDirection.Input,objKey[1]),

                                 		};
            return parameters;
        }
        #endregion ----PrivateSet--
    }
}
